package njci.softerware.com.myexpress2017.serviceObj.dao;

import android.content.Context;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.QueryBuilder;

import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;

import njci.softerware.com.myexpress2017.serviceObj.PackageDispatcher;
import njci.softerware.com.myexpress2017.serviceObj.PackageRecevie;
import njci.softerware.com.myexpress2017.serviceObj.PackageSign;

public class PackageSignDao {
    private Context context;
    private Dao<njci.softerware.com.myexpress2017.serviceObj.PackageSign, Integer> packageSignDao;
    private DatabaseHelper helper;

    public PackageSignDao(Context context) {
        this.context = context;
        try {
            helper = DatabaseHelper.getHelper(context);
            packageSignDao = helper.getDao(njci.softerware.com.myexpress2017.serviceObj.PackageSign.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 增加一个用户
     *
     * @param packageSign
     * @throws SQLException
     */
    public void add(njci.softerware.com.myexpress2017.serviceObj.PackageSign packageSign) {
        try {
            packageSignDao.create(packageSign);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void addList(List<njci.softerware.com.myexpress2017.serviceObj.PackageSign> list) {
        Iterator<njci.softerware.com.myexpress2017.serviceObj.PackageSign> it = list.iterator();
        while (it.hasNext()) {
            njci.softerware.com.myexpress2017.serviceObj.PackageSign packageSign = it.next();
            PackageSign pold;
            if ((pold = getByUserIdAndDate(packageSign.getUser_id(), packageSign.getSign_date())) == null && packageSign.getCount_sign() > 0) {
                this.add(packageSign);
            } else if (packageSign != null && packageSign.getCount_sign() > pold.getCount_sign() && packageSign != null) {
                pold.setCount_sign(packageSign.getCount_sign());
                updateExpressUser(pold);
            }
        }
    }

    public njci.softerware.com.myexpress2017.serviceObj.PackageSign get(int id) {
        try {
            return packageSignDao.queryForId(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void delteExpressUser(njci.softerware.com.myexpress2017.serviceObj.PackageSign user) {
        try {
            packageSignDao.delete(user);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public void updateExpressUser(njci.softerware.com.myexpress2017.serviceObj.PackageSign user) {
        try {
            packageSignDao.update(user);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public List<njci.softerware.com.myexpress2017.serviceObj.PackageSign> findAllUser() {
        List<njci.softerware.com.myexpress2017.serviceObj.PackageSign> list = null;
        try {
            list = packageSignDao.queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }


    public njci.softerware.com.myexpress2017.serviceObj.PackageSign getByUserIdAndDate(Integer user_id, String sign_date) {
        PackageSign p = null;
        try {
            List<njci.softerware.com.myexpress2017.serviceObj.PackageSign> listUser = packageSignDao.queryBuilder().where().eq("user_id", user_id).and().like("sign_date", sign_date).query();
            if (listUser != null && listUser.size() > 0) {
                p = listUser.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return p;
    }

    public List<njci.softerware.com.myexpress2017.serviceObj.PackageSign> getByUserIdAndDate(Integer user_id) {
        List<njci.softerware.com.myexpress2017.serviceObj.PackageSign> listUser = null;
        try {
            listUser = packageSignDao.queryBuilder().where().eq("user_id", user_id).query();
            if (listUser != null && listUser.size() > 0) {
                return listUser;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return listUser;
    }

    public Integer getListCount(int user_Id, String dataArgs, List<String> weekList) {
        int count = 0;
        try {
            QueryBuilder builder = packageSignDao.queryBuilder();
            List<PackageSign> listUser = null;
            if (weekList != null && weekList.size() > 0) {
                listUser = builder.where().eq("user_id", user_Id).and().eq("sign_date", weekList.get(0)).or().eq("sign_date", weekList.get(1)).or().eq("sign_date", weekList.get(2)).or().
                        eq("sign_date", weekList.get(3)).or().eq("sign_date", weekList.get(4)).or().eq("sign_date", weekList.get(5)).or().eq("sign_date", weekList.get(6)).query();
            } else {
                listUser = builder.where().eq("user_id", user_Id).and().like("sign_date", dataArgs).query();
            }
            if (listUser != null && listUser.size() > 0) {
                Iterator<PackageSign> it = listUser.iterator();
                while (it.hasNext()) {
                    PackageSign p = it.next();
                    count = count + p.getCount_sign();
                }
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    public List<PackageSign> getListbySelection(int user_Id, String dataArgs, List<String> weekList) {
        List<PackageSign> listUser = null;
        try {
            QueryBuilder builder = packageSignDao.queryBuilder();
            if (weekList != null && weekList.size() > 0) {
                listUser = builder.where().eq("user_id", user_Id).and().eq("sign_date", weekList.get(0)).or().eq("sign_date", weekList.get(1)).or().eq("sign_date", weekList.get(2)).or().
                        eq("sign_date", weekList.get(3)).or().eq("sign_date", weekList.get(4)).or().eq("sign_date", weekList.get(5)).or().eq("sign_date", weekList.get(6)).query();
            } else {
                listUser = builder.where().eq("user_id", user_Id).and().like("sign_date", dataArgs).query();
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return listUser;
    }

}
